package com.fadadianqi.logistics.customer.dao.ibatis;

import com.fadadianqi.commons.GenericDAOSuport;
import com.fadadianqi.logistics.customer.dao.MsgQueueDAO;
import com.fadadianqi.logistics.customer.model.MsgQueue;
import com.yeqiangwei.commons.util.Pagination;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class MsgQueueDAOImpl extends GenericDAOSuport<String, MsgQueue>
        implements MsgQueueDAO {

    @Override
    protected Class<?> getObjectClass() {
        return MsgQueue.class;
    }

    @Override
    protected String getTableName() {
        return "msg_queue";
    }

    @Override
    public List<MsgQueue> findByJobIdAndStatus(String jobId, int status,
                                               Pagination pagination) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("msgJobId", jobId);
        map.put("status", status);
        map.put("pagination", pagination);
        return super.findByPropertys(map);
    }

    @Override
    public List<MsgQueue> findByJobId(String jobId, Pagination pagination) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("msgJobId", jobId);
        map.put("pagination", pagination);
        return super.findByPropertys(map);
    }

    @Override
    public int countByJobId(String id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("msgJobId", id);
        return super.countAll(map);
    }

    @Override
    public int countQueueByJobIdAndStatus(String jobId, int status) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("msgJobId", jobId);
        map.put("status", status);
        return super.countAll(map);
    }

    @Override
    public void cancelQueueByJobId(String jobId) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("jobId", jobId);
        map.put("status", MsgQueue.STATUS_CANCEL);
        getSqlMapClientTemplate().update(getNamespace() + ".cancelQueueByJobId", map);
    }

    @Override
    public void removeByJobId(String jobId) {
        getSqlMapClientTemplate().delete(getNamespace() + ".removeByJobId", jobId);
    }
}
